import {
  API
} from "../../utils/const"
import {
  formatTime
} from "../../utils/dateUtils"
import {
  getRequest,
  postRequest
} from "../../utils/request"
const app = getApp();
// pages/usinList/useingList.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    app: app,
    usingEquipList: [],
    equipStatus: [],
    equipStatusEnum: {},
    selectEquipList: []
  },
  bindSearchInput(e) {
    console.log(e);
    var val = e.detail.value.trim();
    this.setData({
      searchStr: val
    })
    this.myUsingList(val)
  },
  onAllPolluted: function (e) {
    // this.myUsingList()
    postRequest(API.changeWaitConfirm, {}).then(res => {
      if (res) {
        wx.showToast({
          title: '一键回收已完成',
        })
        this.myUsingList(this.data.searchStr);
      }
    })
  },
  onEquipClick: function (e) {
    console.log(this.selectComponent('.equiplist'))
    var selectItem = e.currentTarget.dataset.item;
    var selectEquipList = this.data.selectEquipList;
    if (selectEquipList.indexOf(selectItem.id) == -1) {
      selectEquipList.push(selectItem.id);
    } else {
      selectEquipList.splice(selectEquipList.indexOf(selectItem.id), 1);
    }
    console.log("selectEquipList", selectEquipList)
    this.setData({
      selectEquipList: [...selectEquipList]
    })
  },
  onEquipChangeStatus(e) {
    const {
      op,
      id
    } = e.currentTarget.dataset;
    postRequest(API.equipOp, {
      equipInstanceId: id,
      useType: op
    }).then((res) => {
      // console.log("result",res);
      if (res) {

        this.myUsingList(this.data.searchStr);
      } else {
        wx.showToast({
          title: '操作失败，请联系管理员',
          icon: "error"
        })
      }
    })
  },
  fetchAllEquipStatus() {
    getRequest(API.equipStatus).then(res => {
      if (res && res.length > 0) {
        const resEnum = {}
        res.forEach(element => {
          resEnum[element.name] = element.text
        });
        this.setData({
          equipStatus: res,
          equipStatusEnum: resEnum
        })
      }

    })
  },
  formatTime: function (date) {
    console.log("jsdate", date)
    return formatTime(date);
  },
  confirmUse: function (e) {

  },
  onLongTapEquip: function (e) {
    const {
      item
    } = e.currentTarget.dataset
    console.log("item", item)
    const that = this;

  },
  doOperateChange: function (params) {
    wx.showActionSheet({
      itemList: ['归还', '丢失'],
      success(res) {
        const {
          tapIndex
        } = res;
        console.log("tableIndex", tapIndex, item)
        if (tapIndex === 0) {
          postRequest(API.equipOp, {
            equipInstanceId: item.id,
            useType: "RETURN"
          }).then(res => {
            if (res) {
              wx.showToast({
                title: `${item.equipName}已发起归还`,
                icon: "success",
                complete: () => {
                  that.myUsingList(that.data.searchStr);

                }
              })
            }
          })

        }
        if (tapIndex === 1) {

        }
        if (tapIndex === 2) {

        }
        // change
        // console.log(res.tapIndex)
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  onEquipBeUse: function () {

  },
  doListQuery: function (e) {
    return getRequest(API.equipUsingList, e.detail).then(res => {
      this.setData({
        usingEquipList: res
      })
      return res;
    })
  },
  myUsingList: function (searchStr) {
    return getRequest(API.equipUsingList, searchStr ? {
      searchStr
    } : {}).then(res => {
      this.setData({
        usingEquipList: res
      })
      return res;
    })
  },
  backClick: function () {
    wx.switchTab({
      url: '/pages/euqip/equip',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.fetchAllEquipStatus();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.myUsingList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})